package com.mego.tzadd.database.manager;


import android.database.sqlite.SQLiteException;

import com.mego.tzadd.activitys.AppContext;
import com.mego.tzadd.database.dbmodel.Thmlog;
import com.mego.tzadd.database.greendao.ThmlogDao;

import java.util.ArrayList;


public class ThmlogCenter {

    private static ThmlogCenter instance = new ThmlogCenter();

    public static ThmlogCenter getInstance() {
        return instance;
    }

    public ArrayList<Thmlog> mThmlogList = new ArrayList<Thmlog>();
    public boolean mNeedlj = true;//需要拦截
    public String mZfmobile = ""; //转发号码

    //查找历史记录
    public Thmlog getThmlog(long date, int type) {
        Thmlog thmlog = null;
        try {
            ThmlogDao systemAllInfoDao = AppContext.getInstances().getDaoSession().getThmlogDao();
            ArrayList<Thmlog> thmList = new ArrayList<Thmlog>();
            thmList = (ArrayList) systemAllInfoDao.queryBuilder()
                    .where(ThmlogDao.Properties.Date.eq(date))
                    .where(ThmlogDao.Properties.Type.eq(type))
                    .build().list();
            if (thmList.size() > 0) {
                thmlog = thmList.get(0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return thmlog;
    }

    public Long saveThmlog(Thmlog thmlog) {
        if (thmlog == null) {
            return 0L;
        }

        long id = 0;
        try {
            ThmlogDao thmlogDao = AppContext.getInstances().getDaoSession().getThmlogDao();
            id = thmlogDao.insertOrReplace(thmlog);
            thmlog.setId(id);
        } catch (SQLiteException e) {
            e.printStackTrace();
        }

        return id;
    }

}
